package com.lge.clock.alarmclock;

import java.lang.reflect.Field;

import com.lge.clock.util.Log;

import android.content.Intent;

public class PouchMode {
	
	private static String TAG = "PouchMode";
	
	public static final int EXTRA_DOCK_STATE_POUCH = 3;
	
	private static Class classIntent;
	private static Field mDocState;
	private static int mPouchMode;
	static int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
	
	public static boolean getDockState(Intent intent){
		boolean mPouch = true;
		
		try {
			classIntent = Class.forName("android.content.Intent");
			
			mDocState = classIntent.getField("EXTRA_DOCK_STATE_POUCH");
			
			mPouchMode = Integer.parseInt(mDocState.get(classIntent).toString());
			
		} catch (ClassNotFoundException e) {
            e.printStackTrace();
            Log.d(TAG, "ClassNotFoundException");
        } catch (SecurityException e) {
            e.printStackTrace();
            Log.d(TAG, "SecurityException");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            Log.d(TAG, "NoSuchFieldException");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            Log.d(TAG, "NumberFormatException");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            Log.d(TAG, "IllegalArgumentException");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            Log.d(TAG, "IllegalAccessException");
		} finally {
			if ( intent.getAction().equals( Intent.ACTION_DOCK_EVENT)) {
            	mDockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
                        Intent.EXTRA_DOCK_STATE_UNDOCKED);
            	if(mDockState == mPouchMode){
            		mPouch = true;
            	}else{
            		mPouch = false;
            	}
            	Log.e(TAG, "mPouch => " + mPouch);
            }
		}
		
		return mPouch;
	}

}
